package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DUserLicenseService;
import cn.huiyunche.base.service.mappers.DUserLicenseMapper;
import cn.huiyunche.base.service.model.DUserLicense;
import cn.huiyunche.base.service.model.DUserLicenseExample;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.service.base.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/12/8 下午3:10
 */
@Service
public class DUserLicenseServiceImpl implements DUserLicenseService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DUserLicenseServiceImpl.class);

    @Autowired
    private DUserLicenseMapper dUserLicenseMapper;

    @Override
    public List<DUserLicense> getListByUserIds(List<Long> dUserIds) {
        LOGGER.info("getListByUserIds params : {}", dUserIds);
        if (CollectionUtils.isEmpty(dUserIds)) {
            LOGGER.error("getListByUserIds params dUserIds must not empty");
            throw new IllegalArgumentException("用户主键不能为空");
        }

        DUserLicenseExample example = new DUserLicenseExample();
        example.createCriteria().andUserIdIn(dUserIds);

        return dUserLicenseMapper.selectByExample(example);
    }

    public int insertSelective(DUserLicense dUserLicense) {
        return dUserLicenseMapper.insertSelective(dUserLicense);
    }

    public int deleteByUserId(Long userId) {
        LOGGER.info("deleteByUserId params : {}", userId);
        DUserLicenseExample example = new DUserLicenseExample();
        example.createCriteria().andUserIdEqualTo(userId);
        int rescord = dUserLicenseMapper.deleteByExample(example);
        LOGGER.info("deleteByUserId rescord : {}", rescord);
        return rescord;

    }

    @Override
    public DUserLicense selectByUserId(Long userId) {
        LOGGER.info("DUserLicenseServiceImpl.selectByUserId param : {}", userId);

        if (null == userId && 0 == userId.longValue()) {
            LOGGER.error("DUserLicenseServiceImpl.selectByUserId param userId must not be null");
            throw new IllegalArgumentException("用户主键不能为空");
        }

        DUserLicenseExample dUserLicenseExample = new DUserLicenseExample();
        dUserLicenseExample.createCriteria().andUserIdEqualTo(userId);

        List<DUserLicense> licenses = dUserLicenseMapper.selectByExample(dUserLicenseExample);
        if (CollectionUtils.isNotEmpty(licenses)) {
            return licenses.get(0);
        }

        return null;
    }
}
